
#include <string.h>

#define log_err(fmt, args...) fprintf(stderr, fmt "\n", ## args)
#define log_error(fmt, args...) fprintf(stderr, "[%s-%s:%d]-[Error] " fmt "\n", __FILE__, __func__, __LINE__, ## args)

#define log_fat(fmt, args...) {fprintf(stderr, fmt "\nAPPLICATION TERMINATED!\n", ## args);exit(EXIT_FAILURE);}
#define log_fatal(fmt, args...) {fprintf(stderr, "[%s-%s:%d]-[Fatal] " fmt "\nAPPLICATION TERMINATED!\n", __FILE__, __func__, __LINE__, ## args);exit(EXIT_FAILURE);}

#undef log_debug             /* undef it, just in case */
#ifdef DEBUG

#include <FileHelpers.h>
#include <stdio.h>

char* file;
char* func;
FILE* fdebug;

#define set_and_print(str1,str2,str3,str4) {str1 = (char*)str2;fprintf(fdebug, str3 ": %s\n", str4);}

#define log_debug_open do {/**/						\
if(fdebug==NULL){									\
	if((fdebug=fopen("debug.txt","w"))==NULL)		\
		log_fatal("Can not open debug file!\n");	\
}													\
} while (0)											\

#define log_debug_close if(fdebug != NULL) {if(fclose(fdebug)) log_fatal("file close Error!\n");}

#define print_func do {/**/													\
	if(func==NULL) set_and_print(func,__func__,"Func:",func );				\
	if(strcmp(func,__func__)) set_and_print(func,__func__,"Func:",func );	\
} while (0)																	\

#define print_file do {/**/																		\
	if(file==NULL) set_and_print(file,__FILE__,"\nFile:",fh_getBaseName(__FILE__) );			\
	if(strcmp(file,__FILE__)) set_and_print(file,__FILE__,"\nFile:",fh_getBaseName(__FILE__) );	\
} while (0)																						\


#define log_deb(fmt, args...) {log_debug_open;fprintf(fdebug, fmt "\n", ## args);}
#define log_debug(fmt, args...) {log_debug_open;print_file; print_func; fprintf(fdebug, "[LINE:%d]-[Debug] " fmt "\n", __LINE__, ## args); }
#else
#define log_debug_open
#define log_debug_close
#define log_deb(fmt, args...) /* not debugging: nothing */
#define log_debug(fmt, args...) /* not debugging: nothing */
#endif


